加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[bigdata-109] spring-cloud-05 分布式服务的ribbon消费者

发布时间:2020-12-14 03:27:04 所属栏目:大数据 来源:网络整理
导读:1. 以ribbon方式实现消费者 2. 参考文档 https://github.com/dyc87112/SpringCloudBook/tree/master/ribbon-consumer 3. 源码结构 ├── pom.xml ├── src │ ? ├── main │ ? │ ? ├── java │ ? │ ? │ ? └── com │ ? │ ? │ ? ? ? └──

1. 以ribbon方式实现消费者


2. 参考文档

https://github.com/dyc87112/SpringCloudBook/tree/master/ribbon-consumer


3. 源码结构

├── pom.xml
├── src
│ ? ├── main
│ ? │ ? ├── java
│ ? │ ? │ ? └── com
│ ? │ ? │ ? ? ? └── brian
│ ? │ ? │ ? ? ? ? ? └── demo
│ ? │ ? │ ? ? ? ? ? ? ? └── serviceconsumerribbon
│ ? │ ? │ ? ? ? ? ? ? ? ? ? ├── App.java
│ ? │ ? │ ? ? ? ? ? ? ? ? ? └── HelloService.java
│ ? │ ? └── resources
│ ? │ ? ? ? └── application.properties


4. pom.xml

<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.brian.demo</groupId>
	<artifactId>serviceconsumerribbon</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>serviceconsumerribbon</name>
	<url>http://maven.apache.org</url>

	<repositories>
		<repository>
			<id>my-nexus-central</id>
			<name>my local nexus</name>
			<url>http://localhost:8081/nexus/content/repositories/central/</url>
			<releases>
				<enabled>true</enabled>
				<updatePolicy>never</updatePolicy>
			</releases>
		</repository>
	</repositories>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.7.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>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-ribbon</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-netflix-hystrix-amqp</artifactId>
		</dependency>


		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Brixton.SR5</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>

</project>


5. App.java

package com.brian.demo.serviceconsumerribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class App {
	
	@Bean
	@LoadBalanced
	RestTemplate restTemplate() {
		return new RestTemplate();
	}

	@Autowired
	HelloService helloService;

	@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
	public String helloConsumer() {
		return helloService.hello();
	}

	public static void main(String[] args) {
		SpringApplication.run(App.class,args);
	}

}


6. HelloService.java

package com.brian.demo.serviceconsumerribbon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;
    
    @HystrixCommand(fallbackMethod="helloFallback",commandKey="helloKey")
    public String hello() {
    	StringBuilder result = new StringBuilder();
        result.append(restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody()).append("<br>");
        return result.toString();
    }
    
    public String helloFallback() {
        return "error";
    }

}


7.?application.properties

spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/,http://localhost:1112/eureka/
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000


8. 编译打包

mvn clean package


9. 运行

java -jar target/serviceconsumerribbon-0.0.1-SNAPSHOT.jar


10. 显示

在浏览器输入http://localhost:9000/ribbon-consumer,能看到获取到的hello结果。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读