`
raymond.chen
  • 浏览: 1418228 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

服务提供者和消费者

 
阅读更多

一、服务提供者

    1、pom.xml文件关键配置

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.7.RELEASE</version>
	<relativePath/>
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>

<dependencies>
	<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-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<build>
  <plugins>
	  <plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
	  </plugin>
	  
	  <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
	   </plugin>
  </plugins>
</build>

 

    2、application.properties文件关键配置

spring.application.name=service-provider-1
server.port=8001

eureka.instance.hostname=${spring.cloud.client.ip-address}
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
#eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://192.168.134.134:7001/eureka/
#eureka.client.service-url.defaultZone=http://root:123456@192.168.134.134:7001/eureka/

 

    3、启动类

@EnableEurekaClient
@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

 

    4、Controller类

@RestController
@RequestMapping("/user")
public class UserController {
	@GetMapping("/{id}")
	public String addUser(@PathVariable(value="id") Long id){
		return "provider >> id=" + id;
	}

	@GetMapping("/query")
	public String query(@RequestParam(value="name") String name){
		return "provider >> name=" + name;
	}
	
	@PostMapping("/add")
	public User add(@RequestBody Address address){
		System.out.println(address.getProvince() + ", " + address.getCity());
		User user = new User();
		user.setId(100L);
		user.setUsername("uid");
		user.setPassword("pwd");
		return user;
	}
}

 

二、服务消费者

    1、pom.xml文件的关键配置

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.7.RELEASE</version>
	<relativePath/>
</parent>

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
	<java.version>1.8</java.version>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-openfeign</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>
</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Finchley.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<build>
  <plugins>
	  <plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
	  </plugin>
	  
	  <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
	   </plugin>
  </plugins>
</build>

 

    2、application.properties文件配置

spring.application.name=service-consumer-1
server.port=9001

eureka.instance.hostname=${spring.cloud.client.ip-address}
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

eureka.client.service-url.defaultZone=http://192.168.134.134:7001/eureka/
#eureka.client.service-url.defaultZone=http://root:123456@192.168.134.134:7001/eureka/

 

    3、启动类

        客户端服务调用采用Feign组件,需要在启动类引入 @EnableFeignClients 注解类。

@EnableFeignClients
@SpringBootApplication
public class Main {
	public static void main(String[] args) {
		SpringApplication.run(Main.class, args);
	}
}

 

    4、编写Feign客户端接口类

@FeignClient(name="service-provider-1") //name值要与服务提供者工程中application.properties文件的spring.application.name属性值一致
@RequestMapping("/user")
public interface ServiceRemote {
	//接口方法的结构要与服务提供者Controller定义的一致
	@GetMapping("/{id}")
	String addUser(@PathVariable(value="id") Long id);
	
	@GetMapping("/query")
	String query(@RequestParam(value="name") String name);
	
	@PostMapping("/add")
	User add(@RequestBody Address address);
}

 

    5、Controller类

        在Controller类引入Feign客户端接口类,调用接口类的方法,Feign组件底层负责服务提供者方法的调用。

@RestController
public class User3Controller {
	@Autowired
    ServiceRemote serviceRemote;
	
	@GetMapping("/consumer")
	public String test(){
		String result = serviceRemote.addUser(22L);
		System.out.println("result=" + result);

		result = serviceRemote.query("cjm");
		System.out.println("result=" + result);
		
		Address address = new Address();
		address.setProvince("GuangDong");
		address.setCity("GuangZhou");
		
		User user = serviceRemote.add(address);
		System.out.println(user.getId() + ", " + user.getUsername() + ", " + user.getPassword());
		
		return "ok";
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics