天天看點

第三篇:搭建服務提供者

一:建立父工程springclould-parent

父工程的打包方式:pom

父工程鎖定springboot和spring-cloud的版本

父工程的pom檔案如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<!--springboot版本号-->

<

parent

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter-parent</

artifactId

>

<

version

>2.0.3.RELEASE</

version

>

<

relativePath

/> 

<!-- lookup parent from repository -->

</

parent

>

<

properties

>

<

project.build.sourceEncoding

>UTF-8</

project.build.sourceEncoding

>

<

project.reporting.outputEncoding

>UTF-8</

project.reporting.outputEncoding

>

<

java.version

>1.8</

java.version

>

<

spring-cloud.version

>Finchley.RELEASE</

spring-cloud.version

>

</

properties

>

<!--鎖定springcloud的版本-->

<

dependencyManagement

>

<

dependencies

>

<

dependency

>

<

groupId

>org.springframework.cloud</

groupId

>

<

artifactId

>spring-cloud-dependencies</

artifactId

>

<

version

>${spring-cloud.version}</

version

>

<

type

>pom</

type

>

<

scope

>import</

scope

>

</

dependency

>

</

dependencies

>

</

dependencyManagement

>

<

dependencies

>

<

dependency

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter</

artifactId

>

</

dependency

>

<!--無論服務提供者還是服務消費者都需要tomcat運作,都是web工程-->

<

dependency

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter-web</

artifactId

>

</

dependency

>

<!--無論服務提供者還是服務消費者,他們都是eureka用戶端-->

<

dependency

>

<

groupId

>org.springframework.cloud</

groupId

>

<

artifactId

>spring-cloud-starter-netflix-eureka-client</

artifactId

>

</

dependency

>

<

dependency

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-starter-test</

artifactId

>

<

scope

>test</

scope

>

</

dependency

>

</

dependencies

>

<

build

>

<

plugins

>

<

plugin

>

<

groupId

>org.springframework.boot</

groupId

>

<

artifactId

>spring-boot-maven-plugin</

artifactId

>

</

plugin

>

</

plugins

>

</

build

>

</

project

>

二:Service Provider服務提供者

1:建立springcloud-service-provider子子產品

2:繼承父工程

3:修改application.properties配置檔案

1

2

3

4

5

6

7

8

#允許注冊eureka注冊中心,預設值就是true

eureka.client.register-with-eureka=true

#允許從eureka注冊中心找服務,并調用,預設值就是true

eureka.client.fetch-registry=true

#eureka的server位址,如果eureka服務多個用逗号隔開

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

#應用名

spring.application.name=spring-cloud-service-provider

4:主程式加上@EnableEurekaClient注解

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package

com.wendao.provider;

import

org.springframework.boot.SpringApplication;

import

org.springframework.boot.autoconfigure.SpringBootApplication;

import

org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient

//或者@EnableDiscoveryClient

public

class

ProviderApplication {

public

static

void

main(String[] args) {

SpringApplication.run(ProviderApplication.

class

, args);

}

}

5:啟動該子產品,通路http://localhost:8761/(注冊中心ui界面位址)看到如下界面,代表服務已經注冊成功。

第三篇:搭建服務提供者

6:暴露一個rest api

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

package

com.wendao.provider.pojo;

public

class

User {

private

String username;

private

int

age;

public

User() {

}

public

String getUsername() {

return

username;

}

public

void

setUsername(String username) {

this

.username = username;

}

public

int

getAge() {

return

age;

}

public

void

setAge(

int

age) {

this

.age = age;

}

public

User(String username, 

int

age) {

super

();

this

.username = username;

this

.age = age;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package

com.wendao.provider.web;

import

com.wendao.provider.pojo.User;

import

org.springframework.web.bind.annotation.PathVariable;

import

org.springframework.web.bind.annotation.RequestMapping;

import

org.springframework.web.bind.annotation.RestController;

@RestController

public

class

ProviderController {

@RequestMapping

(value = 

"/user/{uesrId}"

)

public

User findUserById(

@PathVariable

Integer uesrId) {

//調用service層擷取使用者資訊,這步省略...

return

new

User(

"朱錫銘"

12

);

}

}

繼續閱讀