zoukankan      html  css  js  c++  java
  • springcloud project created

    1. Create a maven simple web project in idea.
    2. Create a module in the simple maven project.
      1. The moudle name is eureka,it's a discovery and register services.
      2. server:
          port: 8761
        
        eureka:
          instance:
            hostname: localhost
          client:
            registerWithEureka: false
            fetchRegistry: false
            serviceUrl:
              defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
        

          yml 配置文件,没有yml后缀的,直接添加一个文件即可

      3.  在application中加入注解@EnableEurekaServer,申明此处为服务注册中心。

        package com.bcd.cloud.pf.eureka;
        
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
        
        /**
         * EurekaApplication class
         *
         * @EnableEurekaServer was created by myself
         *
         * @author myself
         * @date 2019/12/05
         */
        @SpringBootApplication
        @EnableEurekaServer
        public class EurekaApplication {
        
        	public static void main(String[] args) {
        		SpringApplication.run(EurekaApplication.class, args);
        	}
        
        }
        

          Start this project,and view in the web explorer.http://localhost:8761/

      4. You will see this view.
    3. create productor.
      1. This project is a web, created project type is 
        EnableEurekaClient
      2. eureka:
          client:
            serviceUrl:
              defaultZone: http://localhost:8761/eureka/
        server:
          port: 8763
        spring:
          application:
            name: productor
        

          productor was set eureka client,it will discovery by 8761,but its port is 8763.

      3. package com.bcd.cloud.pf.productor;
        
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
        
        /**
         * ProductorApplication class
         *
         * @EnableEurekaClient was created by myself
         *
         * @author myself
         * @date 2019/12/05
         */
        @SpringBootApplication
        @EnableEurekaClient
        public class ProductorApplication {
        
        	public static void main(String[] args) {
        		SpringApplication.run(ProductorApplication.class, args);
        	}
        
        }
        

          

      4. The APPController was added by myself.
        package com.bcd.cloud.pf.productor.rest;
        
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.bind.annotation.RestController;
        
        /**
         * Demo class
         *
         * @author keriezhang
         * @date 2016/10/31
         */
        @RestController
        public class AppController {
        
            @Value("${server.port}")
            String port;
        
            @RequestMapping("/hi")
            public String home(@RequestParam String name)
            {
                return "hi " + name + ",i am from port:" + port;
            }
        }
        

          

      5. Run this application,you will see this view.
    4. create consume. invoke above project that productor.
      1.   The project name is ribbon.瑞本;丝带、条状物,这里用来做断路由使用;他调用上面的productor 生产者。
        <dependencies>
        		<dependency>
        			<groupId>org.springframework.cloud</groupId>
        			<artifactId>spring-cloud-starter-netflix-ribbon</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-netflix-eureka-client</artifactId>
        		</dependency>
        		<!--断路器-->
        		<dependency>
        			<groupId>org.springframework.cloud</groupId>
        			<artifactId>spring-cloud-starter-hystrix</artifactId>
        			<version>1.4.4.RELEASE</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework.boot</groupId>
        			<artifactId>spring-boot-starter-test</artifactId>
        			<scope>test</scope>
        			<exclusions>
        				<exclusion>
        					<groupId>org.junit.vintage</groupId>
        					<artifactId>junit-vintage-engine</artifactId>
        				</exclusion>
        			</exclusions>
        		</dependency>
        	</dependencies>
        

          this is its pom.xml  ,add some dependency.

      2. yml file like this:
        eureka:
          client:
            serviceUrl:
              defaultZone: http://localhost:8761/eureka/
        server:
          port: 8764
        spring:
          application:
            name: ribbon
        

          This project's port is 8764.

      3. Infact this project is a web project also.
        1.   
          package com.bcd.cloud.pf.ribbon.service;
          
          import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.http.ResponseEntity;
          import org.springframework.stereotype.Service;
          import org.springframework.web.client.RestTemplate;
          
          @Service
          public class HelloService {
          
              @Autowired
              RestTemplate restTemplate;
          
              // 断路器配置,当无法调用如下方法时,就会调用自定的hiError方法。
              @HystrixCommand(fallbackMethod = "hiError")
              public String hiService(String name)
              {
                  ResponseEntity<Object> forEntity=restTemplate.getForEntity("http://PRODUCTOR/hi?name=" + name, Object.class);
                  Object forObject=restTemplate.getForObject("http://PRODUCTOR/hi?name=" + name, Object.class);
                  return restTemplate.getForObject("http://PRODUCTOR/hi?name=" + name, String.class);
              }
          
          
              public String hiError(String name) {
                  return "hi,"+name+",This has error in that page.";
              }
          }
          
          hiService invoke the 8763 method hi,you could see this url:
          "http://PRODUCTOR/hi?name=" 

            run this application you will see this view:

        2. The above methd is view that the 8763 server is running,if you stop the 8763 project,you will see this view:
        3. The reason is that :

      4. The ribbon config code like this:
        package com.bcd.cloud.pf.ribbon;
        
        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
        import org.springframework.cloud.client.loadbalancer.LoadBalanced;
        import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
        import org.springframework.cloud.netflix.hystrix.EnableHystrix;
        import org.springframework.context.annotation.Bean;
        import org.springframework.web.client.RestTemplate;
        
        
        /**
         * RibbonApplication class
         *
         * @EnableEurekaClient was created by myself
         * @EnableHystrix was created by myself ,断路由
         *
         * @author myself
         * @date 2019/12/05
         */
        @SpringBootApplication
        //@EnableEurekaClient
        @EnableDiscoveryClient
        @EnableHystrix
        public class RibbonApplication {
        
        	public static void main(String[] args) {
        		SpringApplication.run(RibbonApplication.class, args);
        	}
        
        	/**
        	  *  加入restTemplate以消费相关的服务。
        	* */
        	@Bean
        	@LoadBalanced
        	RestTemplate restTemplate()
        	{
        		return new RestTemplate();
        	}
        
        }
        

          

    www.beicaiduo.com
  • 相关阅读:
    uva 11488
    探测器-旅行者1号:百科
    理论-生命起源理论:百科
    定律:目录
    定律:百科
    理论:目录
    理论(哲学):百科
    理论:百科
    汉语-词语:潮汐
    物理-电磁-电磁相互作用:百科
  • 原文地址:https://www.cnblogs.com/hoge66/p/11986894.html
Copyright © 2011-2022 走看看