Spring cloud(二)服务提供与消费以及载均衡(Feign+Ribbon)

一、环境
jdk:12
spring cloud:Greenwich.RELEASE
spring boot:2.1.0.RELEASE
spring-cloud-starter-feign:2.0.0.M2

二、创建服务并启动双份
1.在前面提到的工程eurekaClient中新建一个服务HelloService.java

package com.bry.springcloud.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloService {

	@Value("${eureka.instance.hostname}")
	String server;
	
	@Value("${server.port}")
	String port;
	
	
	@RequestMapping("/hello")
	public String sayHello(String name) {
		return "Hello " + name + " from "+server + ":" + port ;
	}
}

2.把eurekaClient工程中application.yml文件中的服务端口临时改成6546后打包。

spring:
  application:
    name: eurekaClient
server:
  port: 6546
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://${eureka.instance.hostname}:6543/eureka/

3.启动打包好的在6546端口监听服务

$ d:/opt/jdk12/bin/java -jar eurekaClient-0.0.1-SNAPSHOT.jar

4.将端口改回6544后在IDE中启动eurekaClient

  1. 查看服务中心可以看到EUREKACIENT服务有两个提供者了。
    在这里插入图片描述

三、创建服务消费者

  1. 创建一个新的maven工程 serviceConsumer。pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.bry.springcloud</groupId>
	<artifactId>service_consumer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>service_consumer</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
		<relativePath />
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>12</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.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>2.0.0.M2</version>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.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>
		</plugins>
	</build>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

2.配置文件/serviceConsumer/src/main/resources/application.yml

spring:
  application:
    name: serviceConsumer
server:
  port: 6545
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:6543/eureka/

3.建立启动类ServiceConsumerApplication.java

package com.bry.springcloud.eurekaclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

加@EnableFeignClients表示开启Feign功能

4.定义定义一个feign接口HelloService.java

package com.bry.springcloud.eurekaclient;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "eurekaClient")
public interface HelloService {

	@RequestMapping(value ="/hello",method = RequestMethod.GET)
	public String sayHello(@RequestParam(value = "name") String name) ;

}

eurekaClient为服务提供者的aplication名

6.创建服务器消费类TestService.java

package com.bry.springcloud.eurekaclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestService {
	
	@Autowired
	HelloService helloService;
	
	
	@RequestMapping("/testhello")
	public String sayHello(String name) {
		return helloService.sayHello(name) ;
	}

}

6.启动服务后查看结果
http://localhost:6545/testhello?name=bruce 的返回结果会在【Hello bruce from 127.0.0.1:6544】 和 【Hello bruce from 127.0.0.1:6546】来回切换,说明Ribbon起作用了。
在这里插入图片描述
在这里插入图片描述

参考:
https://blog.csdn.net/forezp/article/details/69808079
http://www.voidcn.com/article/p-srdiezpt-bsb.html
https://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign

备注:
1.服务名不能有下划线
2.可以从 https://github.com/zhoupinheng/springclouddemo 下载完整代码。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值